/**
 * 
 */
package com.zc.BankCreditManagement.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.zc.BankCreditManagement.DAO.CustomerDAO;
import com.zc.BankCreditManagement.DAO.UserDAO;
import com.zc.BankCreditManagement.JavaBean.Customer;
import com.zc.BankCreditManagement.Service.CustomerService;
import com.zc.BankCreditManagement.ZCUtil.GetRanNum;

/**
 * @author ZhangCheng
 * @time 2019年9月17日
 */
public class CustomerServiceImpl implements CustomerService {
	CustomerDAO cusDAO;

	public CustomerServiceImpl() {
		this.cusDAO = new CustomerDAO();
	}

	@Override
	public int insertNewToDB(Customer cus) {
		return this.cusDAO.insertNewCus(cus);
	}

	/**
	 * 删除该用户
	 * 
	 * @return 1：已经被删除 0：删除失败
	 */
	@Override
	public int deleteThisToDB(int cusId,boolean excludeDelete) {
		if (this.cusDAO.getCustomerInfoById(cusId, excludeDelete) != null) {
			return this.cusDAO.deleteCusById(cusId);
		} else {
			System.out.println("删除失败！您的客户信息有误，不存在该用户或已经被删除，请检查您的信息！");
		}
		return 0;
	}

	/**
	 * 获取该用户信息
	 * 
	 * @return
	 * 
	 *         null：没有该客户信息
	 */
	@Override
	public Customer getThisInfoById(int cusId,boolean excludeDelete) {
		return this.cusDAO.getCustomerInfoById(cusId, excludeDelete);
	}

	/**
	 * 获取所有客户的信息
	 * 
	 * @return 所有客户的集合
	 * 
	 */
	@Override
	public ArrayList<Customer> getAllSuitable(boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from Customer ");
		if (excludeDelete)
			sql.append("isdelete = 1");
		return this.cusDAO.getCustomersBySQLFromDB(sql.toString());
	}

	/**
	 * 修改该用户信息
	 * 
	 * @return 1：已经被修改 0：修改失败
	 */
	public int updateCustomer(Customer cus,boolean excludeDelete) {
		if (this.cusDAO.getCustomerInfoById(cus.getCusId(), excludeDelete) != null) {
			return this.cusDAO.updateCusInfo(cus);
		} else {
			System.out.println("更新失败！您的客户信息有误，不存在该用户，请检查您的信息！");
		}
		return 0;
	}
	/**
	 * 注册验证
	 *
	 */
	public String regiVerify(int cusId, String email) {
		Customer c = this.cusDAO.getCustomerInfoById(cusId, true);
		if(c != null) {
			if(email.equals(c.getEmail())) {
				return "Success 正在向您的邮箱发送验证码，请稍候";
			}else {
				return "您的邮箱输入的不正确！";
			}
		}else {
			return null;
		}
	}
	/**
	 * 邮箱验证
	 * @param userId用户名 email邮箱地址
	 */
	public String verifyEmail(String userId,String email,boolean excludeDelete) {
		UserDAO uDAO = new UserDAO();
		int cusId =uDAO.getUserInfoById(userId, excludeDelete).getPeoId();
		String rEmail = cusDAO.getCustomerInfoById(cusId, excludeDelete).getEmail();
		System.out.println(cusId+" "+rEmail+" "+email);
		if(rEmail.equals(email)) {
			return "Success "+cusId + " 您的邮箱输入正确！";
		}else {
			return "您的邮箱无法与您的账号进行匹配！";
		}
	}
	/**
	 * 随机生成客户Id
	 * 
	 */
	public int generateCusId() {
		int cusId;
		while(true) {
			String id = ""+new SimpleDateFormat("YYYY").format(new Date())+"01"+new GetRanNum().RanNum(4);
			cusId = Integer.parseInt(id);
			if(cusDAO.getCustomerInfoById(cusId, false)==null) {
				break;
			};
		}
		return cusId;
		
	}

}
